#!/bin/bash

# ZCache status
# 2019-7-10: modified by xia.jingjing

ZCUSER=`id -un`
BASEDIR="/tmp/$ZCUSER"
LOGFILE="${BASEDIR}/zc-status.log"

declare -A KEYMAP

################################ FUNCTION #####################################

function LockFile() {   
    if [ ! -f $LOCK_FILE ]; then
        echo $$>$LOCK_FILE
    else
        sh_pid=`cat $LOCK_FILE`
        p_cn=`ps -ef |awk '{if ($2 == '$sh_pid') print $3}'|wc -l`
        if [ $p_cn -gt 0 ]; then
            return 1
        fi
        echo $$>$LOCK_FILE
    fi
    return 0
}

function UnlockFile() {
    rm -rf ${LOCK_FILE} || true
}

function warn { 
    errmsg=$(tail -1 $ERRFILE)
    if [ "$errmsg" != "" ]; then
        echo -en `date +'%F %T'`" # "$@": " >> $LOGFILE
        echo "$errmsg" >> $LOGFILE
    fi
    
}

DEFAULT_VALUE="uptime_in_seconds:-1\n"\
"connected_clients:0\n"\
"used_memory:0\n"\
"used_memory_rss:0\n"\
"used_memory_peak:0\n"\
"mem_fragmentation_ratio:0\n"\
"rdb_changes_since_last_save:0\n"\
"rdb_bgsave_in_progress:0\n"\
"rdb_last_save_time:-1\n"\
"rdb_last_bgsave_status:fail\n"\
"rdb_current_bgsave_time_sec:-1\n"\
"aof_enabled:0\n"\
"aof_rewrite_scheduled:0\n"\
"aof_last_rewrite_time_sec:-1\n"\
"aof_current_rewrite_time_sec:-1\n"\
"aof_last_bgrewrite_status:fail\n"\
"aof_current_size:0\n"\
"aof_base_size:0\n"\
"aof_pending_rewrite:0\n"\
"aof_buffer_length:0\n"\
"aof_rewrite_buffer_length:0\n"\
"aof_pending_bio_fsync:0\n"\
"aof_delayed_fsync:0\n"\
"total_connections_received:0\n"\
"total_commands_processed:0\n"\
"instantaneous_ops_per_sec:0\n"\
"rejected_connections:0\n"\
"expired_keys:0\n"\
"evicted_keys:0\n"\
"keyspace_hits:0\n"\
"keyspace_misses:0\n"\
"latest_fork_usec:0\n"\
"connected_slaves:0\n"\
"master_link_status:0\n"\
"master_last_io_seconds_ago:0\n"\
"master_sync_in_progress:0\n"\
"used_cpu_sys:0\n"\
"used_cpu_user:0\n"\


function redis_status() {
    LOCK_FILE="$BASEDIR/redis-$SERV-$PORT.lock"
    ERRFILE="${BASEDIR}/error-$SERV-$PORT.tmp"
        
    for i in {1..10}  
    do  
        LockFile
        if [ $? -eq 0 ]; then
            currcache=$(redis-cli -h $SERV -p $PORT $AUTHINFO --raw INFO 2>${ERRFILE})
            if [ $? -ne 0 ]; then
                warn "connect redis ${SERV}:${PORT} error"
            else
                currcache=$(echo "$currcache" | sed "s/\r//g")
            fi            
            UnlockFile
            break
        fi
        sleep 0.02
    done    
    if [ "$currcache" = "" ]; then
        currcache="$DEFAULT_VALUE"
    fi
   
    KEYMAP["uptime_in_seconds_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^uptime_in_seconds:" |awk -F':|,' '{print $2}')
    KEYMAP["rdb_changes_since_last_save_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^rdb_changes_since_last_save:" |awk -F':|,' '{print $2}')
    KEYMAP["rdb_bgsave_in_progress_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^rdb_bgsave_in_progress:" |awk -F':|,' '{print $2}')
    KEYMAP["rdb_last_save_time_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^rdb_last_save_time:" |awk -F':|,' '{print $2}')  
    KEYMAP["rdb_current_bgsave_time_sec_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^rdb_current_bgsave_time_sec:" |awk -F':|,' '{print $2}') 
    rdb_last_bgsave_status=$(echo -e "$currcache" | grep "^rdb_last_bgsave_status:" |awk -F':|,' '{print $2}')
    if [ "$rdb_last_bgsave_status" = "ok" ]; then
        rdb_last_bgsave_status="1"
    else
        rdb_last_bgsave_status="0"
    fi
    KEYMAP["rdb_last_bgsave_status_${SERV}_${PORT}"]=$rdb_last_bgsave_status
    
    aof_last_bgrewrite_status=$(echo -e "$currcache" | grep "^aof_last_bgrewrite_status:" |awk -F':|,' '{print $2}')
    if [ "$aof_last_bgrewrite_status" = "ok" ]; then
        aof_last_bgrewrite_status="1"
    else
        aof_last_bgrewrite_status="0"
    fi
    KEYMAP["aof_last_bgrewrite_status_${SERV}_${PORT}"]=$aof_last_bgrewrite_status    
    KEYMAP["aof_enabled_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_enabled:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_rewrite_scheduled_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_rewrite_scheduled:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_last_rewrite_time_sec_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_last_rewrite_time_sec:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_current_rewrite_time_sec_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_current_rewrite_time_sec:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_current_size_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_current_size:" |awk -F':|,' '{print $2}')    
    KEYMAP["aof_base_size_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_base_size:" |awk -F':|,' '{print $2}')    
    KEYMAP["aof_pending_rewrite_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_pending_rewrite:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_buffer_length_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_buffer_length:" |awk -F':|,' '{print $2}')    
    KEYMAP["aof_rewrite_buffer_length_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_rewrite_buffer_length:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_pending_bio_fsync_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_pending_bio_fsync:" |awk -F':|,' '{print $2}')
    KEYMAP["aof_delayed_fsync_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^aof_delayed_fsync:" |awk -F':|,' '{print $2}')     
    KEYMAP["total_connections_received_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^total_connections_received:" |awk -F':|,' '{print $2}')
    KEYMAP["total_commands_processed_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^total_commands_processed:" |awk -F':|,' '{print $2}')
    KEYMAP["instantaneous_ops_per_sec_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^instantaneous_ops_per_sec:" |awk -F':|,' '{print $2}')
    KEYMAP["rejected_connections_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^rejected_connections:" |awk -F':|,' '{print $2}')
    KEYMAP["latest_fork_usec_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^latest_fork_usec:" |awk -F':|,' '{print $2}') 
    KEYMAP["expired_keys_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^expired_keys:" |awk -F':|,' '{print $2}')  
    KEYMAP["evicted_keys_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^evicted_keys:" |awk -F':|,' '{print $2}')   
    KEYMAP["keyspace_hits_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^keyspace_hits:" |awk -F':|,' '{print $2}')   
    KEYMAP["keyspace_misses_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^keyspace_misses:" |awk -F':|,' '{print $2}')
    KEYMAP["connected_slaves_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^connected_slaves:" |awk -F':|,' '{print $2}')
   
    master_link_status=$(echo -e "$currcache" | grep "^master_link_status:" |awk -F':|,' '{print $2}')
    if [ "$master_link_status" = "up" ]; then
        master_link_status="1"
    else
        master_link_status="0"
    fi
    KEYMAP["master_link_status_${SERV}_${PORT}"]=$master_link_status
    KEYMAP["master_last_io_seconds_ago_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^master_last_io_seconds_ago:" |awk -F':|,' '{print $2}')
    KEYMAP["master_sync_in_progress_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^master_sync_in_progress:" |awk -F':|,' '{print $2}')  
    KEYMAP["used_cpu_sys_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^used_cpu_sys:" |awk -F':|,' '{print $2}')
    KEYMAP["used_cpu_user_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^used_cpu_user:" |awk -F':|,' '{print $2}')  
    KEYMAP["used_memory_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^used_memory:" |awk -F':|,' '{print $2}')
    KEYMAP["used_memory_rss_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^used_memory_rss:" |awk -F':|,' '{print $2}')
    KEYMAP["used_memory_peak_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^used_memory_peak:" |awk -F':|,' '{print $2}')
    KEYMAP["mem_fragmentation_ratio_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^mem_fragmentation_ratio:" |awk -F':|,' '{print $2}') 
    KEYMAP["connected_clients_${SERV}_${PORT}"]=$(echo -e "$currcache" | grep "^connected_clients:" |awk -F':|,' '{print $2}')
}


###############################################################################

SERVERS="$1"
AUTH="$2"

if [ "$SERVERS" = "" -a -f zcache.ini ]; then
    source zcache.ini
fi

if [ "$SERVERS" = "" ]; then
    echo "Please set SERVERS"
    exit 1
fi
if [ "$AUTH" != "" ]; then
    AUTHINFO="-a $AUTH"
fi

if [ ! -d "$BASEDIR" ]; then
    mkdir -p $BASEDIR
fi

for server in `echo "$SERVERS" | sed 's/,/ /g'`
do
    SERV=`echo $server|awk -F':' '{print $1}'`
    PORT=`echo $server|awk -F':' '{print $2}'`
    
    redis_status

done

for key in `echo ${!KEYMAP[@]} | sed "s/ /\n/g" | sort`
do
    value=${KEYMAP[$key]}
    if [ "$value" = "" ]; then
        echo "$key:0"
    else
        echo "$key:$value"
    fi
done


